package student.stu23002124.chapter13.UDPThread;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestReceive  extends Thread{
    public void run() {
        try {
            //创建socket相当于创建码头
            DatagramSocket socket = new DatagramSocket(6666);
            //创建packet相当于创建集装箱
            DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
            while(true) {
                socket.receive(packet);//接收货物
                byte[] arr = packet.getData();
                int len = packet.getLength();
                String ip = packet.getAddress().getHostAddress();
                System.out.println(ip + ":" + new String(arr,0,len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new TestReceive().run();
        return;
    }
}
